package com.zgs.mall.product.service.impl;

import com.zgs.mall.product.entity.SkuImagesEntity;
import com.zgs.mall.product.entity.SpuInfoDescEntity;
import com.zgs.mall.product.service.*;
import com.zgs.mall.product.vo.SkuItemSaleAttrVo;
import com.zgs.mall.product.vo.SkuitemVo;
import com.zgs.mall.product.vo.SpuItemAttrGroupVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zgs.common.utils.PageUtils;
import com.zgs.common.utils.Query;

import com.zgs.mall.product.dao.SkuInfoDao;
import com.zgs.mall.product.entity.SkuInfoEntity;
import org.springframework.util.StringUtils;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {
    @Autowired
    private SkuImagesService skuImagesService;
    @Autowired
    private SpuInfoDescService spuInfoDescService;
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private SkuSaleAttrValueService skuSaleAttrValueService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuInfu(SkuInfoEntity skuInfoEntity) {
        this.baseMapper.insert(skuInfoEntity);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {


        QueryWrapper<SkuInfoEntity> queryWrapper = new QueryWrapper<>();
        String key = (String)params.get("key");
        if(!StringUtils.isEmpty(key)){
            queryWrapper.and((w)->{
                w.eq("sku_id",key).or().like("sku_name",key);
            });
        }
        String brandId = (String)params.get("brandId");
        if(!StringUtils.isEmpty(brandId)&&!"0".equals(brandId)){
            queryWrapper.eq("brand_id",brandId);
        }
        String catelogId = (String)params.get("catelogId");
        if(!StringUtils.isEmpty(catelogId)&&!"0".equals(catelogId)){
            queryWrapper.eq("catalog_id",catelogId);
        }
        //price
        Integer min = Integer.valueOf((String)params.get("min"));
        Integer max = Integer.valueOf((String)params.get("max"));
        if(min>0){
            queryWrapper.ge("price",min);
        }
        if(max>0){
            queryWrapper.le("price",max);
        }
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    public List<SkuInfoEntity> queryBySpuId(Long spuId) {
        List<SkuInfoEntity> entityList = this.list(new QueryWrapper<SkuInfoEntity>().eq("spu_id", spuId));
        return entityList;
    }
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;
    //商品详情
    @Override
    public SkuitemVo item(Long skuId) throws ExecutionException, InterruptedException {

        SkuitemVo skuitemVo =new SkuitemVo();
        CompletableFuture<SkuInfoEntity> infoFuture = CompletableFuture.supplyAsync(() -> {
            //sku 基本信息 pms_sku_info
            SkuInfoEntity infoEntity = this.getById(skuId);
            skuitemVo.setSkuInfo(infoEntity);
            return infoEntity;
        }, threadPoolExecutor);
       //thenAcceptAsync 等待 infoFuture 执行完再执行
        CompletableFuture<Void> saleAttrFuture = infoFuture.thenAcceptAsync((res) -> {
            Long spuId = res.getSpuId();
            //spu 的销售属性组合
            List<SkuItemSaleAttrVo> skuItemSaleAttrVos = skuSaleAttrValueService.getSaleAttrBySpuId(spuId);
            skuitemVo.setAttrVos(skuItemSaleAttrVos);
        }, threadPoolExecutor);

        CompletableFuture<Void> desFuture = infoFuture.thenAcceptAsync((res -> {
            Long spuId = res.getSpuId();
            //商品介绍 pms_spu_desc
            SpuInfoDescEntity entity = spuInfoDescService.getById(spuId);
            skuitemVo.setSpuInfoDesc(entity);
        }), threadPoolExecutor);

        CompletableFuture<Void> baseAttrFuture = infoFuture.thenAcceptAsync((res -> {
            Long spuId = res.getSpuId();
            Long catalogId = res.getCatalogId();
            //获取spu的规格参数信息
            List<SpuItemAttrGroupVo> spuItemAttrGroupVos = attrGroupService.getAttrGroupWithAttrsBySpuId(catalogId, spuId);
            skuitemVo.setGroupAttrs(spuItemAttrGroupVos);
        }), threadPoolExecutor);

        CompletableFuture<Void> imaFuture = CompletableFuture.runAsync(() -> {
            //sku 图片信息 pms_sku_images
            List<SkuImagesEntity> skuImagesEntitys = skuImagesService.getImagesBySkuId(skuId);
            skuitemVo.setImages(skuImagesEntitys);
        }, threadPoolExecutor);

        //等待所有任务
        CompletableFuture.allOf(saleAttrFuture,
                desFuture,baseAttrFuture,
                imaFuture).exceptionally((e)->{
                return null;
        }).get();
        return skuitemVo;
    }

}